var validator;
var $problemFeedbackUpdateForm = $("#problemFeedback-update-form");
$(function () {
    validateRule();
    $("#problemFeedback-update .btn-save").click(function () {
        validator = $problemFeedbackUpdateForm.validate();
        var flag = true;
        if (flag) {
            $.post(ctx + "problemFeedback/update", $problemFeedbackUpdateForm.serialize(), function (r) {
                if (r.code === 0) {
                    closeModal();
                    refresh();
                    $MB.n_success(r.msg);
                } else $MB.n_danger(r.msg);
            });
        }
    });

    $("#problemFeedback-update .btn-close").click(function () {
        closeModal();
    });

});



function closeModal() {
    $("#problemFeedback-update-button").attr("name", "save");
    $("#problemFeedback-update-modal-title").html('新增字典');
    validator.resetForm();
    $MB.closeAndRestModal("problemFeedback-update");
}

function validateRule() {
    var icon = "<i class='zmdi zmdi-close-circle zmdi-hc-fw'></i> ";
    validator = $problemFeedbackAddForm.validate({
        rules: {
            problemRecord: {
                required: true,
                maxlength: 127
            },
            expectedResults: {
                required: true,
                maxlength: 127
            },
            enclosure: {
                required: true,
                maxlength: 127
            }
        },
        messages: {
            problemRecord: {
                required: icon + "请输入问题",
                maxlength: icon + "长度不能超过127个字符"
            },
            expectedResults: {
                required: icon + "请输入预期结果",
                maxlength: icon + "长度不能超过127个字符"
            },
            enclosure: {
                // required: icon + "请输入项目代号",
                maxlength: icon + "长度不能超过127个字符"
            }
        }
    });
}



function updateProblemFeedback() {
    var selected = $("#problemFeedbackTable").bootstrapTable("getSelections");
    var selected_length = selected.length;
    if (!selected_length) {
        $MB.n_warning('请勾选需要修改的项目！');
        return;
    }
    if (selected_length > 1) {
        $MB.n_warning('一次只能修改一个项目！');
        return;
    }
    var id = selected[0].id;
    $.post(ctx + "problemFeedback/get", {"id": id}, function (r) {
        if (r.code === 0) {
            var $form = $('#problemFeedback-update');
            $form.modal();
            var problemFeedback = r.msg;
            $("#problemFeedback-update-modal-title").html('修改反馈');
            $form.find("input[name='id']").val(problemFeedback.id);
            $form.find("input[name='problemRecord']").val(problemFeedback.problemRecord);
            $form.find("input[name='expectedResults']").val(problemFeedback.expectedResults);

            $("input:radio[value='" + problemFeedback.enclosureTag + "']").prop("checked", true);

            $form.find("input[name='enclosure']").val(problemFeedback.enclosure);
            $form.find("input[name='createTime']").val(problemFeedback.createTime);
            $("#problemFeedback-update-button").attr("name", "update");
        } else {
            $MB.n_danger(r.msg);
        }
    });
}